package oiunt.hello;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ImgSwitchResizeScroll extends Activity{
	private ImageView imgBody;	
	private Bitmap resizeBitmap;
	private BitmapDrawable bmd;
	private Bitmap bitmapOrg;
	private BitmapFactory.Options mOptions;
	private Menu contextMenu;
	
	private Resources r;
	
	private static final Integer[] IMAGE_LIST = { R.drawable.front,
		R.drawable.back, R.drawable.side };
	private static int index = 0;
	private static final int LEN = IMAGE_LIST.length;
	
//	private static final float[] ZOOM_RATE = {0.2f, 0.4f, 0.6f, 0.8f, 1.0f, 1.2f, 1.4f, 1.6f, 1.8f, 2.0f};
	private static final float[] ZOOM_RATE = {0.2f, 0.4f, 0.6f, 0.8f, 1.0f};
	private static final int ZOOM_LEN = ZOOM_RATE.length;
	
	/**
	 * default is 1.0f
	 */
	private static int zoomIndex = 2;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		setContentView(R.layout.switchandscroll);	
		
		mOptions = new BitmapFactory.Options();
        mOptions.inDither = false;
        mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
//        mOptions.inSampleSize = 2;
//        mOptions.inTempStorage = new byte[750 * 1500];
        
        r = getResources();		
				
		imgBody = (ImageView) findViewById(R.id.bodyImg);
		
		setImage();
	}
	
	
	private void setImage() {		
		try {		
//			ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//			ActivityManager.MemoryInfo mem = new ActivityManager.MemoryInfo(); 
//			am.getMemoryInfo(mem);
//			
//			long a = mem.availMem;
//			boolean b= mem.lowMemory;

			resizeBitmap = null;
			bmd = null;
			bitmapOrg = null;
			imgBody.setImageDrawable(null);
			System.gc();
			
//			am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//			mem = new ActivityManager.MemoryInfo(); 
//			am.getMemoryInfo(mem);			
//			a = mem.availMem;
//			b= mem.lowMemory;
			
			Matrix matrix = new Matrix();
			matrix.postScale(ZOOM_RATE[zoomIndex], ZOOM_RATE[zoomIndex]);			
			
			System.gc();
			
			bitmapOrg = BitmapFactory.decodeResource(r,IMAGE_LIST[index], mOptions);
			//recreate the bitmap
			resizeBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
					bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
//			resizeBitmap = Bitmap.createScaledBitmap(bitmapOrg, dstWidth, dstHeight, true);
			bmd = new BitmapDrawable(resizeBitmap);
			imgBody.setImageDrawable(bmd);

//			am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//			mem = new ActivityManager.MemoryInfo(); 
//			am.getMemoryInfo(mem);					
//			a = mem.availMem;
//			b= mem.lowMemory;
			
			System.gc();
			
		} catch (Exception e) {
//			Log.v("setImage", e.getMessage());
//			System.out.println
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.optionmenu1, menu);
	    contextMenu = menu;
	    return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {				
		switch (item.getItemId()) {
		case R.id.preItem:
			index--;
			if (index < 0) {
				index = LEN - 1;
			}

			setImage();
			return true;
		case R.id.nextItem:
			index++;
			if (index >= LEN) {
				index = 0;
			}

			setImage();
			return true;
		case R.id.zoomInItem:
			zoomIndex++;
			contextMenu.getItem(3).setEnabled(true);
			if (zoomIndex >= ZOOM_LEN - 1) {
				contextMenu.getItem(2).setEnabled(false);
			}

			setImage();
			return true;
		case R.id.zoomOutItem:
			zoomIndex--;
			contextMenu.getItem(2).setEnabled(true);
			if (zoomIndex <= 0) {
				contextMenu.getItem(3).setEnabled(false);
			}

			setImage();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

}
